<?php if(!defined('BASEPATH')) exit('No direct script access allowed!');

if ( !function_exists('my_form_input') ) 
{
	function my_form_label($name, $label, $error='') {
		return '<div class="label '.(!empty($error)?'error':'').'">' . form_label($label, $name) . '</div>'; 
	}
}

if ( !function_exists('my_form_error') ) 
{
	function my_form_error($fieldname) {
		return form_error($fieldname, '<span class="error">', '</span>');
	}
}

if ( !function_exists('my_form_input') ) 
{
	function my_form_input($arrays) {	// name, label, default, properties
		return _my_form_field('input', $arrays);
	}
}

if ( !function_exists('my_form_textarea') ) 
{
	function my_form_textarea($arrays) {	// name, label, default, properties
		return _my_form_field('textarea', $arrays);
	}
}

if ( !function_exists('my_form_password') ) 
{
	function my_form_password($arrays) {	// name, label, properties
		return _my_form_field('password', $arrays);
	}
}

if ( !function_exists('my_form_radio') ) 
{
	//FIXME: set_radio after invalid post return unchecked
	function my_form_radio($arrays) {	// name, label, options, default
		return _my_form_field('radio', $arrays);
	}
}

if ( !function_exists('my_form_dropdown') ) 
{
	function my_form_dropdown($arrays) { // name, label, options, default, properties
		return _my_form_field('dropdown', $arrays);
	}
}

if ( !function_exists('my_form_multiselect') ) 
{
	function my_form_multiselect($arrays) { // name, label, options, default, properties
		return _my_form_field('multiselect', $arrays);
	}
}

if ( !function_exists('my_form_upload') ) 
{
	function my_form_upload($arrays) { // name, label, default, properties
		return _my_form_field('upload', $arrays);
	}
}

if ( !function_exists('_my_form_field') ) 
{
	function _my_form_field($type, $arrays) {
		$error = my_form_error($arrays['name']);

		$output = '<li>';
		$output .= my_form_label($arrays['name'], $arrays['label'], $error);
		$output .= '<div class="input">';
		switch ($type) {
		case 'input':
			$output .= form_input($arrays['name'], set_value($arrays['name'], $arrays['default']), (isset($arrays['properties'])?$arrays['properties']:''));
			break;
		case 'textarea':
			$output .= form_textarea(array('name'=>$arrays['name'], 'value'=>set_value($arrays['name'], $arrays['default']))+(isset($arrays['properties']) && is_array($arrays['properties'])?$arrays['properties']:array()));
			break;
		case 'password':
			$output .= form_password($arrays['name'], set_value('', ''), (isset($arrays['properties'])?$arrays['properties']:''));
			break;
		case 'radio':
			foreach ($arrays['options'] as $opt) {
				$output .= form_radio($arrays['name'], $opt['value'], set_radio($arrays['name'], $opt['value'], $arrays['default']==$opt['value']?TRUE:FALSE), 
											'id="'.$arrays['name'].'_'.$opt['value'].'"') .
								 	 form_label($opt['label'], $arrays['name'].'_'.$opt['value']) . '&nbsp;&nbsp;&nbsp;';
			}
			break;
		case 'dropdown':
			$output .= form_dropdown($arrays['name'], $arrays['options'], set_value($arrays['name'], $arrays['default']), 
										(isset($arrays['properties'])?$arrays['properties']:''));
			break;
		case 'multiselect':
			$output .= form_multiselect($arrays['name'], $arrays['options'], set_value($arrays['name'], $arrays['default']), 
										(isset($arrays['properties'])?$arrays['properties']:''));
			break;
		case 'upload':
			$output .= form_upload($arrays['name'], set_value($arrays['name'], $arrays['default']), (isset($arrays['properties'])?$arrays['properties']:''));
			if (!empty($arrays['default'])) {
				$output .= ' <a href="'.$arrays['default'].'" class="fancybox">tampilkan</a>';
			}
			break;
		}
		$output .= $error;
		$output .= '</div>';
		$output .= '</li>';
		return $output;
	}
}

if ( !function_exists('list_months_for_option') ) 
{
	function list_months_for_option($months=array(), $start_month=1, $current_month='', $include_blank=false, $blank_label='-- Semua --') {
		$str = '';
		if ($include_blank) {
			$str = '<option value="">'.$blank_label.'</option>';
		}
		if (empty($months)) {
			$months = list_months();
		}
		$total = count($months);
		$i = 0;
		while ($i < $total) {
			$str .= '<option value="'.$start_month.'"';
			if ($start_month == $current_month) {
				$str .= ' selected="selected" ';
			}
			$str .= '>' . $months[$i];
			$str .= '</option>';
			$i += 1;
			$start_month += 1;
		}
		return $str;
	}
}
